﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net.Mail;
using System.Configuration;

public partial class profile : BasePage
{
	protected override void pageLoad()
	{
		if (!IsPostBack)
		{
			if (StudentId < 0)
				return;

			//if ((Teacher.IS_ADMIN != 1)&&(Teacher.IS_CALL_CENTER !=1))
			//{
			//    DataLayer dal = new DataLayer();
			//    string message;
			//    bool result = dal.IsMyStudent(Configuration.ConnectionString, Teacher.TEACHER_ID, StudentId, CurrentInfo.SemesterId, out message);
			//    PALogManager.MessageLog(message);
			//    if(!result)
			//    {
			//        Response.Redirect("error.aspx");
			//    }
			//}

			hlEntry.NavigateUrl = string.Format("StudentEntry.aspx?studentId={0}", StudentId);
			Fill();

			bindData();
		}
	}
	protected void bindData()
	{	DataLayer dal = new DataLayer();
			string message = string.Empty;


			Student student = dal.ReadStudent(PAConfiguration.ConnectionString, StudentId, out message);
			PALogManager.MessageLog(message);
			BindStudent(student);

			bindPeriods();
	}
	protected void bindPeriods()
	{
		DateTime current;
		if (Teacher.IS_ADMIN == 0)
			this.ddlPeriods.DataSource = Manager.GetWeeks(DateTime.Now, CurrentInfo.FIRSTDAY, CurrentInfo.LASTDAY, out current);
		else
			this.ddlPeriods.DataSource = Manager.GetWeeksForAdmin(DateTime.Now, CurrentInfo.FIRSTDAY, CurrentInfo.LASTDAY, out current);
				this.ddlPeriods.DataBind();
		if (current != DateTime.MinValue)
			this.ddlPeriods.SelectedValue = current.ToString();


	}

	protected void BindStudent(Student student)
	{
		hlEntry.Text = student.LASTFIRST;
		lAddress.Text = string.Format("{0} {1}, <br> {2} {3}", student.STREET, student.CITY, student.STATE, student.ZIP);
		lGender.Text = student.GENDER;
		lPhone.Text = student.PHONE;
		lEmail.Text = student.EMAIL;
		lHomePhone.Text = student.HOME_PHONE;
		lParentName.Text = student.PARENT_NAME;
		lParentNumber.Text = student.PARENT_PHONE;
		lParentEmail.Text = student.PARENT_EMAIL;
		tbTo.Text = student.EMAIL;
	}
	protected override void Fill()
	{
		DataLayer dal = new DataLayer();
		string message = string.Empty;

		DataTable data = dal.GetLogEntries(PAConfiguration.ConnectionString, StudentId, out message).Tables[0];
		PALogManager.MessageLog(message);

		if (!string.IsNullOrEmpty(Sorting))
		{
			data.DefaultView.Sort = Sorting;
		}

		this.rgLogEntries.DataSource = data;
		this.rgLogEntries.DataBind();
	}

	protected void gvLogEntries_Sorting(object sender, GridViewSortEventArgs e)
	{
		Sort(e);
	}

	protected void Button1_Click(object sender, ImageClickEventArgs e)
	{
		try
		{
			DataLayer dal = new DataLayer();
			string message = string.Empty;

			dal.Excuse_Absence(
				PAConfiguration.ConnectionString,
				Teacher.TEACHER_ID,
				StudentId,
				DateTime.Parse(ddlPeriods.SelectedValue),
				tbMemo.Text,
				out message);

			PALogManager.MessageLog(message);

			if (!string.IsNullOrEmpty(message))
			{
				lError.Text = message;
			}
			else
			{
				lError.Text = "Excused Absence Saved";
			}
		}
		catch (System.Exception ex)
		{
			lError.Text = ex.Message;
		}

		bindData();
		Fill();
	}
	protected void Button2_Click(object sender, ImageClickEventArgs e)
	{
		try
		{
			DataLayer dal = new DataLayer();
			string message = string.Empty;

			dal.Save_Call(
				PAConfiguration.ConnectionString,
				Teacher.TEACHER_ID,
				StudentId,
				tbMessage.Text,
				out message);

			PALogManager.MessageLog(message);

			if (!string.IsNullOrEmpty(message))
			{
				lError.Text = message;
			}
			else
			{
				lError.Text = "Phone Call has been Saved";
			}
		}
		catch (System.Exception ex)
		{
			lError.Text = ex.Message;
		}
		bindData();
		Fill();
	}
	protected void Button3_Click(object sender, ImageClickEventArgs e)
	{
		try
		{
		DataLayer dal = new DataLayer();
		string message = string.Empty;



		MailMessage mailmessage = new MailMessage();
		mailmessage.From = new MailAddress(ConfigurationManager.AppSettings["NotificationEmail"]);
		mailmessage.To.Add(new MailAddress(tbTo.Text));

		mailmessage.Subject = tbSubject.Text;

		mailmessage.Body = tbMessage.Text;

		SmtpClient sc = new SmtpClient();
		sc.EnableSsl = bool.Parse(ConfigurationManager.AppSettings["NotificationEmailEnableSSL"]);
		sc.Send(mailmessage);


		dal.Save_Email(
			PAConfiguration.ConnectionString,
			Teacher.TEACHER_ID,
			StudentId,
			tbTo.Text,
			tbSubject.Text,
			tbMessage.Text,
			out message);

		PALogManager.MessageLog(message);

		if (!string.IsNullOrEmpty(message))
		{
			lError.Text = message;
		}
		else
		{
			lError.Text = "Email has been Sent";
		}
		}
		catch (System.Exception ex)
		{
			lError.Text = ex.Message;
		}

		bindData();
		Fill();
	}

	public override string Icon
	{
		get
		{
			return "images/student-s_profile_icon.png";
		}
	}
	protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
	{
		try
		{
			DataLayer dal = new DataLayer();
			string message = string.Empty;

			dal.AddLogEntriesForProfile(
				PAConfiguration.ConnectionString,
				StudentId,
				Teacher.TEACHER_ID,
				tbMessage.Text,
				decimal.Parse(ddLogEntryType.SelectedValue),
				out message);

			PALogManager.MessageLog(message);

			if (!string.IsNullOrEmpty(message))
			{
				lError.Text = message;
			}
			else
			{
				lError.Text = "Contact has been Saved";
			}
		}
		catch (System.Exception ex)
		{
			lError.Text = ex.Message;
		}
		bindData();
		Fill();
	}

	protected void rgLogEntries_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
	{
		Sort(e);
	}

	protected void rgLogEntries_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
	{
		Fill();
	}
}
